@extends('layouts.adminlayout')

@section('main')
<a href="<?php echo asset('manager-students/createuserscheduleiframe')?>" class="btn btn-info" ><i class="fa fa-user"></i>Thêm tài khoản học viên khóa thi</a>

<section class="content">
    <div class="col-xs-12 col-sm-12 col-md-12 col-lg-12">
        <div class="box box-primary">
            <div class="box-header">
                <h3 class="box-title">Danh sách tài khoản học viên <small>Thông tin cơ bản</small></h3>
                <button class="btn btn-default pull-right" style="margin: 5px 5px 0px 0px;" onclick="frintliststudent()"><i class="fa fa-download"></i> Xuất Excel</button>
                    <br/>
                </div><!-- /.box-header -->
                    <div class="box-body">

                    <table id="dataTable" class="table table-bordered table-striped" >
                        <thead>
                            <tr>
                                <th>Mã khóa thi</th>
                                <th>Họ Tên</th>
                                <th>Ngày Sinh</th>
                                <th>Sđt</th>
                                <th>Email</th>
                                <th>Lệ phí thi</th>
                                <th>Tùy chọn</th>
                            </tr>
                        </thead>
                        <tfoot>
                            <tr>
                                <th>Mã khóa thi</th>
                                <th>Họ Tên</th>
                                <th>Ngày Sinh</th>
                                <th>Sđt</th>
                                <th>Email</th>
                                <th>Lệ phí thi</th>
                                <td></td>

                            </tr>
                        </tfoot>
                        <tbody>
                            @foreach ($results as $result)
                            <tr>
                                <td>{{{ $result->event_name }}}</td>
                                <td>{{{ $result->fullname }}}</td>
                                <td>{{{ $result->birthday }}}</td>
                                <td>{{{ $result->phone }}}</td>
                                <td>{{{ $result->email }}}</td>
                                <td>{{{ $result->fee_schedule}}}</td>
                                <td style="width: 100px!important;">
                                  <a class="btn btn-danger" onclick="edit_user(<?php echo $result->user_id;?>)"><i class='fa fa-edit'></i></a>
                                  <a class="btn btn-info" onclick="print_schedule(<?php echo $result->user_id;?>,<?php echo $result->schedule_id;?>)"><i class='fa fa-print'></i></a>
                                </td>
                            </tr>
                            @endforeach
                        </tbody>
                    </table>
                    </div>
                </div>
            </div>
        </div>
</section>

<div class="modal fade" id="myModal_user" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-body">
       <form class="form-horizontal" action="<?php echo asset('user/infouser'); ?>" method="POST" role="form" name="edit_info" id="edit_info">
                <fieldset>
                    <div class="form-group">
                    <div class="form-group">
                    </div>
                        {{ Form::label('username', 'Tài Khoản:',array('class'=>'col-lg-3 control-label')) }}
                        <div class="col-lg-6">
                            {{ Form::text('username',null,array('class'=>'form-control ','disabled')) }}
                        </div>
                    </div>

                    <div class="form-group">
                        {{ Form::label('password', 'Mật khẩu',array('class'=>'col-lg-3 control-label')) }}
                        <div class="col-lg-6">
                            <input type="password" name="password" id="password" class="form-control" required="required" title="">
                        </div>
                    </div>

                    <div class="form-group">
                        {{ Form::label('fullname', 'Họ tên',array('class'=>'col-lg-3 control-label')) }}
                        <div class="col-lg-6">
                            {{ Form::text('fullname',null,array('class'=>'form-control ')) }}
                        </div>
                    </div>
                    <div class="form-group">
                        {{ Form::label('birthday', 'Ngày Sinh:',array('class'=>'col-lg-3 control-label')) }}
                        <div class="col-lg-6">
                            {{ Form::text('birthday',null,array('class'=>'form-control','id'=>'ngay_sinh_hv')) }}
                        </div>
                        <div class="col-lg-3" id='check-birthday'>
                            
                        </div>
                    </div>
                    <div class="form-group">
                        {{ Form::label('gender', 'Giới Tính:',array('class'=>'col-lg-3 control-label'))}}
                        <div class="col-lg-6"> 
                            <select name="gender" id="gender" class="form-control" required="required">
                                <option value="">Chọn</option>
                                <option value="Nam">Nam</option>
                                <option value="Nữ">Nữ</option>
                            </select>
                        </div>
                    </div>
                    <div class="form-group">
                        {{ Form::label('email', 'Email:',array('class'=>'col-lg-3 control-label')) }}
                        <div class="col-lg-6">
                            {{ Form::text('email',null,array('class'=>'form-control ')) }}
                        </div>
                        <div class="col-lg-3" id='check-email'>
                            
                        </div>
                    </div>
                    <div class="form-group">
                        {{ Form::label('phone', 'Số Điện Thoại:',array('class'=>'col-lg-3 control-label')) }}
                        <div class="col-lg-6">
                            {{ Form::text('phone',null,array('class'=>'form-control ')) }}
                        </div>
                    </div>

                    <div class="form-group">
                        {{ Form::label('address', 'Địa Chỉ:',array('class'=>'col-lg-3 control-label')) }}
                        <div class="col-lg-6">
                            {{ Form::textarea('address',null,array('class'=>'form-control ')) }}
                        </div>
                    </div>
                    <div class="form-group">
                        <div class="col-lg-3 control-label">
                            <label>Trạng thái</label>
                        </div>
                        <div class="col-lg-6" id='div_sta_active'>                           
                        </div>
                    </div>
 
                    <div class="form-group">
                        <div class="col-lg-12" id="noti"></div>
                    </div>
                </fieldset>
        </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">Đóng</button>
        <button type="submit" class="btn btn-primary">Cập Nhật</button>
      </div>
      </form>
    </div>
  </div>
</div>
<div id='table_data'></div>
<script src="<?php echo asset('public/admin/js/plugins/datatables/jquery.dataTables.columnFilter.js'); ?>" type="text/javascript"></script>
<script type="text/javascript">
    $('#dataTable tfoot th').each(function() {
            var title = $('#dataTable thead th').eq($(this).index()).text();
            $(this).html('<input type="text" placeholder="' + title + '" />');
        });

        var table = $('#dataTable').DataTable({
                    "scrollX": "100%",
                    "scrollCollapse": true,
                    "paging": true,
                    "autoWidth": false
                });
        // Apply the filter
        table.columns().eq(0).each(function(colIdx) {
            $('input', table.column(colIdx).footer()).on('keyup change', function() {
                table
                .column(colIdx)
                .search(this.value)
                .draw();
            });
        });
        function edit_user(id){
            $.ajax({
                url: '<?php echo asset("ajax/getuser")?>',
                type: 'post',
                dataType: 'json',
                data: {id: id},
            })
            .done(function(data) {
                for (var key in data) {
                   if (data.hasOwnProperty(key)) {
                    $('#username').val(data[key].username);
                    $('#password').val(data[key].password);
                    $('#fullname').val(data[key].fullname);
                    $('#ngay_sinh_hv').val(data[key].birthday);
                    $('#email').val(data[key].email);
                    $('#gender').val(data[key].gender);
                    $('#phone').val(data[key].phone);
                    $('#address').val(data[key].address);
                    if(data[key].active != 1){
                        $('#div_sta_active').html('<span class="label label-danger" onclick="active('+data[key].id+')">Chưa kích hoạt </i></span>');
                    }else{
                       $('#div_sta_active').html('<span class="label label-info">Đã kích hoạt </i></span>'); 
                    }
                   }
                }
                $('#myModal_user').modal('show');
                console.log("success");
            })
            .fail(function() {
                console.log("error");
            })
            .always(function() {
                console.log("complete");
            });
        }
    var int_email = 0;
    var int_ngaysinh = 0;
    var int_password =0 ;
        $('#password').keyup(function(event) {
        if($('#password').val().toString().length > 6){
            $('#check-password').html('<span class="label label-success">Mật khẩu hợp lệ</i></span>');
            int_password = 0;
        }else{
            $('#check-password').html('<span class="label label-danger">Mật khẩu phải lớn hơn 6 kí tự</i></span>');
            int_password = 1;
        }
    });
    $('#email').keyup(function(event) {
        /* Act on the event */
        var VAL = $(this).val().toString();

        var email = new RegExp(/^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i);

        if(email.test(VAL)){
            $('#check-email').html('<span class="label label-success">E-mail hợp lệ</i></span>');
            int_email =0;
        }else{
            $('#check-email').html('<span class="label label-danger">E-mail không hợp lệ</i></span>');
            int_email = 1;
        }
    });
    $('#ngay_sinh').keyup(function(event) {
        /* Act on the event */
        var val = $(this).val();
        var date = new RegExp(/^(0[1-9]|[12][0-9]|3[01])[- /.](0[1-9]|1[012])[- /.](19|20)\d\d$/);

        if(date.test(val)){
            $('#check-birthday').html('<span class="label label-success">Ngày sinh hợp lệ</i></span>');
            int_ngaysinh = 0;
        }else{
            $('#check-birthday').html('<span class="label label-danger">Ngày sinh không hợp lệ</i></span>');
            int_ngaysinh =1;
        }
    });
    $('#edit_info').submit(function(event) {
        $("#edit_info input").prop("disabled", true);
        $("#edit_info select").prop("disabled", true);
        $("#edit_info textarea").prop("disabled", true);
        $('#noti').html('<br/><div class="alert alert-warning fade in"><p>Đang cập nhật tài khoản</p></div>');
        if(int_ngaysinh == 0 && int_email == 0 && int_password==0 ){
            $.ajax({
                url: '<?php echo asset("manager-students/infousermanager")?>',
                type: 'post',
                dataType: 'json',
                data: {
                    username: $('#username').val(),
                    password: $('#password').val(),
                    email: $('#fullname').val(),
                    birthday: $('#ngay_sinh_hv').val(),
                    email: $('#email').val(),
                    gender: $('#gender').val(),
                    phone: $('#phone').val(),
                    address: $('#address').val()
                },
            })
            .done(function(data) {
                if(data == 1){
                    $('#noti').html('<br/><div class="alert alert-info fade in"><p>Cập nhật tài khoản thành công.</p></div>');
                    $("#edit_info input").prop("disabled", false);
                    $("#edit_info select").prop("disabled", false);
                    $("#edit_info textarea").prop("disabled", false);
                    // window.location.replace('<?php echo asset("/")?>');
                }
            })
            .fail(function() {
                $("#edit_info input").prop("disabled", false);
                $("#edit_info select").prop("disabled", false);
                $("#edit_info textarea").prop("disabled", false);
                $('#noti').html('<br/><div class="alert alert-warning fade in"><p>Thất bại. Vui lòng kiểm tra lại thông tin hoặc tải lại trang</p></div>');
            })
            .always(function() {
                console.log("complete");
            });
        }
        return false;
    });

    function active(id) {
        $.ajax({
            url: '<?php echo asset("manager-students/active")?>',
            type: 'post',
            dataType: 'json',
            data: {id: id},
        })
        .done(function(data) {
            if(data==1){
              $('#div_sta_active').html('<span class="label label-info">Đã kích hoạt </i></span>');   
            }
            console.log("success");
        })
        .fail(function() {
            console.log("error");
        })
        .always(function() {
            console.log("complete");
        });   
    }


    function frintliststudent () {
        var value = $('input[placeholder="Mã khóa học"]').val();
        $.ajax({
            url: '<?php echo asset("manager-students/frintlistschedule")?>',
            type: 'post',
            dataType: 'html',
            data: {event_name: value},
        })
        .done(function(data) {
            console.log("success");
            $('#table_data').html(data);
            $("#table_data").btechco_excelexport({
                containerid: "table_data"
               , datatype: $datatype.Table
            });
        })
        .fail(function() {
            console.log("error");
        })
        .always(function() {
            console.log("complete");
        });
    }

    function print_schedule(user_id,schedule_id){
        $.ajax({
            url: '<?php echo asset("manager-students/getpageschedulefrint")?>',
            type: 'post',
            dataType: 'html',
            data: {user_id: user_id,
                schedule_id: schedule_id},
        })
        .done(function(data) {
            var myWindow = window.open("", "MsgWindow", "width=630, height=600");
            myWindow.document.write(data);
        })
        .fail(function() {
            console.log("error");
        })
        .always(function() {
            console.log("complete");
        });
    }
</script>
@stop